//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../../common/js/ajax.js'); //引入Axios方法
let WxValidate = require('../../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		imgList: [],
		starg: false,
		stare: false,
		starl: false,
		goodscore: '',
		salescore: '',
		logiscore: '',
		signValue: [],
		sign: [],
		order_id: 0
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this
		this.initValidate()
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		this.setData({
			order_id: e.id
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Store/store_label', {}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					sign: data
				})
				_this.data.sign.forEach(function(item) {
					item.select = false;
				})
			} else {
				this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '回评'
		})
	},
	preventTouchMove: function() {

	},
	chooseImage: function(e) {
		var _this = this
		var type = ['album', 'camera'];
		var temporaryImg = []
		wx.chooseImage({
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: type, //可选择性开放访问相册、相机
			success: function(res) {
				temporaryImg = res.tempFilePaths
				d_gui()
			}
		})

		function d_gui() {
			var i = 0
			wx.getImageInfo({
				src: temporaryImg[i],
				success: function(res) {
					var ctx = wx.createCanvasContext('photo_canvas');
					var ratio = 2;
					var canvasWidth = res.width
					var canvasHeight = res.height;
					// 保证宽高均在200以内
					//console.log(canvasWidth)
					if (res.width > res.height) {
						canvasHeight = res.height * (750 / res.width)
						canvasWidth = 750
					} else if (res.height > res.width) {
						canvasWidth = res.width * (1366 / res.height)
						canvasHeight = 1366
					} else {
						while (canvasWidth > 750 || canvasHeight > 1336) {
							//比例取整
							canvasWidth = Math.trunc(res.width / ratio)
							canvasHeight = Math.trunc(res.height / ratio)
							ratio++;
						}
					}
					//console.log(canvasWidth)
					_this.setData({
						canvasWidth: canvasWidth,
						canvasHeight: canvasHeight
					}) //设置canvas尺寸
					ctx.drawImage(temporaryImg[i], 0, 0, canvasWidth, canvasHeight) //将图片填充在canvas上
					ctx.draw()
					//下载canvas图片				
					setTimeout(function() {
						console.log(ctx)
						wx.canvasToTempFilePath({
							canvasId: 'photo_canvas',
							success: function(res) {
								console.log(res.tempFilePath)
								wx.uploadFile({
									url: app.globalData.baseUrl + 'App/PublicCommon/Public/UploadPicture',
									filePath: res.tempFilePath,
									name: 'file',
									formData: {
										uid: _this.uid,
										sign: _this.Md5,
										time: _this.timesp
									},
									success: function(res) {
										var data = JSON.parse(res.data) //坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式
										var info = data.info
										if (data.status == 1) {
											let images = _this.data.imgList
											images.push(info.save_path)
											images = images.length <= 9 ? images : images.slice(0, 9)
											_this.setData({
												imgList: images
											})
											temporaryImg.splice(0, 1)
											if (temporaryImg.length > 0) {
												d_gui()
											}
											temporaryImg.splice(0, 1)
											if (temporaryImg.length > 0) {
												d_gui()
											}
										}
									}
								})
							},
							fail: function(error) {
								console.log(error)
							}
						})
					}, 100)
				},
				fail: function(error) {
					console.log(error)
				}
			})
		}
	},
	deleteFunc: function(e) { //删除
		var _this = this
		var index = e.currentTarget.dataset.id
		var images = this.data.imgList
		images.splice(index, 1)
		_this.setData({
			imgList: images
		})
	},
	handleStatTap: function(e) { //星
		var id = e.currentTarget.dataset.id;
		var type = e.currentTarget.dataset.type;
		if (type == 0) {
			this.setData({
				goodscore: id
			})
			if (id == 0) {
				this.setData({
					starg: !this.data.starg
				})
			} else {
				this.setData({
					starg: true
				})
			}
		}
		if (type == 1) {
			this.setData({
				salescore: id
			})
			if (id == 0) {
				this.setData({
					stare: !this.data.stare
				})
			} else {
				this.setData({
					stare: true
				})
			}
		}
		if (type == 2) {
			this.setData({
				logiscore: id
			})
			if (id == 0) {
				this.setData({
					starl: !this.data.starl
				})
			} else {
				this.setData({
					starl: true
				})
			}
		}

	},
	handleSelectTap: function(e) { //标签
		var index = e.currentTarget.dataset.index;
		var select = this.data.sign[index].select;
		var id = this.data.sign[index].id
		var temp = this.data.signValue
		if (select == false) {
			temp.push(id)
			this.setData({
				signValue: temp,
				['sign[' + index + '].select']: true
			})
		} else {
			var type = temp.indexOf(id)
			temp.splice(type, 1)
			this.setData({
				signValue: temp,
				['sign[' + index + '].select']: false
			})
		}
		console.log(this.data.signValue)
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var _this = this
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5
		}
		var obj = Object.assign(e, CONST);
		console.log(obj)
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_evaluate',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					wx.navigateTo({
						url: '/pages/myOrder/myOrder?culum=6'
					})
				} else {
					_this.showModal(res)
				}
			})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			label_id: {
				required: false,
			},
			store_id: {
				required: false,
			},
			evaluate_pictures: {
				required: false,
			},
			content: {
				required: false,
			},
			store_desc_point: {
				required: true,
			},
			store_service_point: {
				required: true,
			},
			store_logistics_point: {
				required: true,
			}
		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {

			store_desc_point: {
				required: '请给宝贝描述评分',
			},
			store_service_point: {
				required: '请给卖家服务评分',
			},
			store_logistics_point: {
				required: '请给物流服务评分',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
